package de.blitzcoder.collide.config.textarea;

import de.blitzcoder.collide.config.*;
import de.blitzcoder.collide.gui.textarea.CollIDETextArea;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.InputStream;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import org.gjt.sp.jedit.syntax.SyntaxStyle;
import org.gjt.sp.util.SyntaxUtilities;

/**
 *uure
 * @author blitzcoder
 */
public class TextAreaTab extends ConfigTab implements TableCellRenderer {

    private Object[][] data;
    private CollIDETextArea textArea;
    private static final String[] PROPERTIES = {
        "view.style.comment1",
        "view.style.comment2",
        "view.style.comment3",
        "view.style.comment4",
        "view.style.digit",
        "view.style.function",
        "view.style.invalid",
        "view.style.keyword1",
        "view.style.keyword2",
        "view.style.keyword3",
        "view.style.keyword4",
        "view.style.label",
        "view.style.literal1",
        "view.style.literal2",
        "view.style.literal3",
        "view.style.literal4",
        "view.style.markup",
        "view.style.operator"
    };
    private static final String[] BMAXNAMES = {
        "Einzeilenkommentar",
        "Mehrzeilenkommentar",
        "",
        "",
        "Zahl",
        "Funktion/Methode",
        "",
        "",
        "Keyword",
        "",
        "",
        "",
        "String",
        "Type",
        "Konstanten",
        "Globals",
        "",
        "Operator"
    };
    private JTable table;
    private FontPanel fontPanel;
    private Object propertyBackup;


    @Override
    public void save() {
        /*
        for (int i = 0; i < PROPERTIES.length; i++) {
            CollIDETextArea.getPropertyManager().setSyntaxStyleProperty(PROPERTIES[i], ((PreviewPanel) data[i][2]).getStyle());
        }
        CollIDETextArea.getPropertyManager().setProperty("view.font", fontPanel.getSelectedFont());
        CollIDETextArea.getPropertyManager().setProperty("view.fontsize", ""+fontPanel.getSelectedFontSize());
        CollIDETextArea.getPropertyManager().setProperty("view.antiAlias", fontPanel.getAntiAliasEnabled() ? "subpixel" : "none");
         */
    }

    @Override
    public void load() {
        data = new Object[PROPERTIES.length][5];

        for (int x = 0; x < data.length; x++) {

            SyntaxStyle s = SyntaxUtilities.parseStyle(CollIDETextArea.getPropertyManager().getProperty(PROPERTIES[x]), CollIDETextArea.getPropertyManager().getProperty("view.font"), CollIDETextArea.getPropertyManager().getIntProperty("view.fontsize"), true);

            for (int y = 0; y < data[x].length; y++) {
                switch (y) {
                    case 0:
                        data[x][y] = PROPERTIES[x].replace("view.style.", "");
                        break;
                    case 1:
                        data[x][y] = BMAXNAMES[x];
                        break;
                    case 2:
                        PreviewPanel pa = new PreviewPanel(PROPERTIES[x]);
                        data[x][y] = pa;
                        break;
                }
            }
        }

        table.setModel(new TableModel(data));

        table.setDefaultRenderer(JComponent.class, this);
        table.setDefaultEditor(JPanel.class, new TableEditor(this));
    }

    public TextAreaTab() {
        propertyBackup = CollIDETextArea.getPropertyManager().getBackup();
    }

    @Override
    public void cancel() {
        CollIDETextArea.getPropertyManager().restoreBackup(propertyBackup);
        System.out.println("cancel");
    }

    @Override
    public void createInterface() {

        table = new JTable();
        textArea = new CollIDETextArea();

        textArea.setFileExtension("bmx", "blitzmax.xml");

        table.setCellSelectionEnabled(false);
        setLayout(new GridBagLayout());

        fontPanel = new FontPanel(textArea);

        GridBagConstraints c = new GridBagConstraints();

        c.weightx = 1.0;
        c.gridy = 0;
        c.fill = c.BOTH;
        c.gridx = 0;
        c.weighty = 1.0;
        add(new JScrollPane(table), c);

        c.gridy = 1;
        c.weighty = 0.0;
        add(fontPanel,c);

        c.fill = c.BOTH;
        c.gridy = 2;
        c.weighty = 0.0;
        textArea.setMinimumSize(new Dimension(200,150));
        add(textArea, c);

        try {
            InputStream stream = this.getClass().getResourceAsStream("/de/blitzcoder/collide/config/textarea/testcode.bmx");
            byte[] bytes = new byte[stream.available()];
            stream.read(bytes);
            stream.close();
            textArea.setText(new String(bytes));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public String getTitle() {
        return "Highlighter";
    }

    public void update() {
        for (int i = 0; i < PROPERTIES.length; i++) {
            CollIDETextArea.getPropertyManager().setSyntaxStyleProperty(PROPERTIES[i], ((PreviewPanel) data[i][2]).getStyle());
        }
        textArea.propertiesChanged();
        this.repaint();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if (value instanceof Component) {
            return (Component) value;
        } else if (value instanceof String) {
            return new JLabel((String) value);
        }
        return new JLabel("Invalid Cell");
    }
}
